/*
https://leetcode.cn/problems/reverse-subarray-to-maximize-array-value/solution/chun-cui-by-bai-mu-ying-li-dra-eniac-rd49/
 */
public class Solution1330 {
    public int maxValueAfterReverse(int[] nums) {
        int ans=0;
        for (int i=0;i<nums.length-1;i++){
            ans=ans+Math.abs(nums[i+1]-nums[i]);
        }
        int a=0;
        for (int i=0;i<nums.length-1;i++){
            a=Math.max(a,Math.abs(nums[i]-nums[nums.length-1])-Math.abs(nums[i]-nums[i+1]));
        }
        for (int i=0;i<nums.length-1;i++){
            a=Math.max(a,Math.abs(nums[0]-nums[i+1])-Math.abs(nums[i]-nums[i+1]));
        }
        int x=Math.min(nums[0],nums[1]),y=Math.max(nums[0],nums[1]);
        for (int i=1;i<nums.length-1;i++){
            x=Math.max(x,Math.min(nums[i],nums[i+1]));
            y=Math.min(y,Math.max(nums[i],nums[i+1]));
        }
        a=Math.max(a,2*(x-y));
        return ans+a;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1330().maxValueAfterReverse(new int[]{2,3,1,5,4}));
    }
}
